
import { _decorator, Component, Node, Vec3 } from 'cc';
import dataUtil from './dataUtil';
const { ccclass, property } = _decorator;

@ccclass('memoryConfig')
export class memoryConfig extends Component {




    /**
     * 关卡index 从0 开始
     */
    public level: number = 0;

    private static _instance: memoryConfig;
    static get instance() {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new memoryConfig();
        return this._instance;
    }

    private memoryCfg: { [name: string]: any } = {};

    public addObject(config: any) {
        for (let k in config) {
            this.memoryCfg[k] = config[k]
        }
    }
    /**
     * 
     * @param keyName 
     * @param keyValue 
     */
    public addKey(keyName: string, keyValue: any) {
        this.memoryCfg[keyName] = keyValue;
    }
    /**
     * 
     * @param keyName   键名
     * @param defaultValue   值是 null or undefined 时的默认值
     */
    public getKey(keyName: string, defaultValue: any): string {
        var v = this.memoryCfg[keyName];
        if (v == null || v == undefined) {
            return defaultValue;
        }
        return this.memoryCfg[keyName];
    }
    /**
     * 
     * @param keyName 
     * @param keyValue 
     */
    public setKey(keyName: string, keyValue: any) {
        this.memoryCfg[keyName] = keyValue;
    }



    public getIntKey(keyName: string, defaultValue: number): number {
        let v = this.getKey(keyName, defaultValue);
        return parseInt(v);
    }

    public appendInt(keyName: string, appendValue: number, defaultValue: number) {
        let memoryValue = this.getIntKey(keyName, defaultValue);
        memoryValue += appendValue;
        this.setKey(keyName, memoryValue);
    }

    public getFloatKey(keyName: string, defaultValue: number): number {
        let v = this.getKey(keyName, defaultValue);
        return parseFloat(v);
    }

    public appendFloat(keyName: string, appendValue: number, defaultValue: number) {
        let memoryValue = this.getFloatKey(keyName, defaultValue);
        memoryValue += appendValue;
        this.setKey(keyName, memoryValue);
    }

    public getBoolKey(keyName: string): boolean {
        var v = this.memoryCfg[keyName];
        if (v == "true" || v == true) {
            return true;
        }
        return false;
    }
}

